package com.lepu.app.fun.chat.core;

import android.text.TextUtils;
import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.main.activity.MainTabActivity;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Chang on 9/20/2014.
 */
public class XMPPConnectionListener implements ConnectionListener {
    private static final String TAG = XMPPConnectionListener.class.getSimpleName();

    private long userId;
    private String password;
    private String host;
    private int port;
    private IClientChat clientChat;

    public XMPPConnectionListener(IClientChat clientChat, String host, int port, long userId, String password) {
        this.host = host;
        this.port = port;
        this.userId = userId;
        this.password = password;
        this.clientChat = clientChat;
    }

    @Override
    public void connected(final XMPPConnection connection) {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener connected");
    }

    @Override
    public void authenticated(XMPPConnection connection) {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener authenticated");

        clientChat.setConnectionClosed(false);
        clientChat.stopConnectTimer();

        boolean flag = connection.isAuthenticated();
        LogUtilBase.LogD(TAG, "XMPPConnectionListener authenticated isAuthenticated ===>>>" + flag);
    }

    /**
     * 连接正常断开，
     */
    @Override
    public void connectionClosed() {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener connectionClosed");
        clientChat.setConnectionClosed(true);

        clientChat.startConnectTimer();
    }

    /**
     * 连接异常断开
     * @param arg0
     */
    @Override
    public void connectionClosedOnError(Exception arg0) {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener connectionClosedOnError:" + arg0.getMessage());
        clientChat.setConnectionClosed(true);

        if (!TextUtils.isEmpty(arg0.getMessage()) && arg0.getMessage().contains("conflict")) {
            //用来判断是否是已经登录再次登录
            //用户连接着呢，被踢了，就会进入冲突
            //自己登录还被踢，先注释
//            MainTabActivity instance = MainTabActivity.getInstance();
//            if (instance != null) {
//                instance.mHandler.sendEmptyMessage(MainTabActivity.MSG_SHOW_LOGIN_TOKEN_DIALOG);
//            }
        }else{
            clientChat.startConnectTimer();
        }
    }
    @Override
    public void reconnectingIn(int arg0) {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener reconnectingIn:" + arg0);
    }

    @Override
    public void reconnectionFailed(Exception arg0) {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener reconnectionFailed:" + arg0);
    }

    @Override
    public void reconnectionSuccessful() {
        LogUtilBase.LogD(TAG, "XMPPConnectionListener reconnectionSuccessful");
        clientChat.setConnectionClosed(false);

        //重新连接成功后，重新登录
//        if(!clientChat.getConnection().isConnected()){
//            LogUtilBase.LogD(TAG, "XMPPConnectionListener reconnectionSuccessful relogin");
//            clientChat.login(this.host,this.port,this.userId,this.password);
//        }
    }

}

